package com.itheima.service.Impl;

import com.itheima.dao.IUserDao;
import com.itheima.domian.Role;
import com.itheima.domian.UserInfo;
import com.itheima.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao iUserDao;

    @Autowired
    public BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        UserInfo userInfo = null;
        try {
            userInfo = iUserDao.findByUsername(userName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (userInfo.getStatus() == 0) {
            return null;
        }
        //处理自己的用户对象封装成UserDetails
//        User user = new User(userInfo.getUsername(), "{noop}"+userInfo.getPassword(), getAuthority(userInfo.getRoles() )
        User user = new User(
                userInfo.getUsername(),
                userInfo.getPassword() ,
                userInfo.getStatus() == 0 ? false : true,
                true,
                true,
                true,
                getAuthority(userInfo.getRoles())
        );
        return user;
    }

    public  /*Collection<? extends GrantedAuthority> */
    List<SimpleGrantedAuthority>
    getAuthority(List<Role> roles) {
        List<SimpleGrantedAuthority> list = new ArrayList<>();
        for (Role role : roles
        ) {
            list.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        return list;
    }


    @Override
    public List<UserInfo> findAll() throws  Exception{
        return iUserDao.findAll();
    }


    @Override
    public void save(UserInfo userInfo) throws Exception {
        //对密码进行加密处理
        userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
        iUserDao.save(userInfo);
    }

    @Override
    public UserInfo findById(String id) throws Exception {
        return iUserDao.findById(id);
    }

    @Override
    public List<Role> findOtherRoles(String userid) throws Exception {
        return iUserDao.findOtherRole(userid);
    }

    @Override
    public void addRoleToUser(String userId, String[] roleIds) throws Exception {
        for (String roleId: roleIds
             ) {
            iUserDao.addRoleToUser(userId,roleId);
        }

    }

//    @Override
//    public UserInfo findByIdper(String id) throws Exception {
//        return iUserDao.findByUsernamePer(id);
//    }


}
